package com.neu.admin;

import com.neu.common.GoodsAllinone;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.domain.ShoppingGoods;
import com.neu.domain.ShoppingGoodsAttribute;
import com.neu.domain.ShoppingGoodsProduct;
import com.neu.domain.ShoppingGoodsSpecification;
import com.neu.service.ShoppingCategoryService;
import com.neu.service.ShoppingGoodsService;
import com.neu.util.response.BaseResponse;
import com.neu.util.response.ListVoUntil;
import com.neu.vo.ShoppingCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {
    @Autowired
    private ShoppingGoodsService shoppingGoodsService;
    @Autowired
    private ShoppingCategoryService shoppingCategoryService;

    @ApiOperation(value = "分页查询商品")
    @GetMapping("/list")
    public ResponseEntity<BaseResponse<ListVoUntil<ShoppingGoods>>> list(
            @RequestParam("limit") Integer limit,
            @RequestParam("page") Integer page,
            @Sort @RequestParam(value = "add_time",required = false) String sort,
            @Order @RequestParam(value = "desc",required = false) String order,
            @RequestParam(value = "goodsSn",required = false) Integer goodsSn,
            @RequestParam(value = "name",required = false) String name
    ){
//        try {
        //获取用户列表

        List<ShoppingGoods> goodsVoList = shoppingGoodsService.list(limit,page,sort,order,goodsSn,name);
        return BaseResponse.generateOKListResponseEntity(goodsVoList);
//        }catch (Exception e){
//            log.debug("GoodsController -->>ResponseEntity",e);
//            return BaseResponse.generateBadResponseEntity(500,"服务器内部错误","");
//        }special
    }
    @ApiOperation(value = "查询商品详情")
    @GetMapping("/detail")
    public ResponseEntity<BaseResponse> detail(ShoppingGoods shoppingGoods, ShoppingGoodsSpecification shoppingGoodsSpecification,
                                               ShoppingGoodsProduct shoppingGoodsProduct, ShoppingGoodsAttribute shoppingGoodsAttribute) {

        ShoppingGoods goodsVoList = shoppingGoodsService.detail(shoppingGoods.getId());
        List<ShoppingGoodsSpecification> shoppingGoodsSpecificationList  = shoppingGoodsService.specifications(shoppingGoodsSpecification);
        List<ShoppingGoodsProduct> shoppingGoodsProduct1 = shoppingGoodsService.products(shoppingGoodsProduct);
        List<ShoppingGoodsAttribute> shoppingGoodsAttributeList = shoppingGoodsService.attributes(shoppingGoodsAttribute);
        Integer categoryId = shoppingGoods.getCategoryId();
        ShoppingCategory category = shoppingCategoryService.findById(categoryId);
        Integer[] categoryIds = new Integer[]{};
        if (category != null) {
            Integer parentCategoryId = category.getPid();
            categoryIds = new Integer[]{parentCategoryId, categoryId};
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("goods", goodsVoList);
        data.put("specifications", shoppingGoodsSpecificationList);
        data.put("products",shoppingGoodsProduct1);
        data.put("attributes",shoppingGoodsAttributeList);
        data.put("categoryIds", categoryIds);
        return BaseResponse.generateOKResponseEntity(data);
    }
    @ApiOperation(value = "新增商品详情")
    @PostMapping("/create")
    public Object create(@RequestBody  GoodsAllinone goodsAllinone) {
        return  shoppingGoodsService.create(goodsAllinone);
    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/delete")
    public ResponseEntity<BaseResponse<ShoppingGoods>> delete(@RequestBody ShoppingGoods shoppingGoods) {

        int count = shoppingGoodsService.delete(shoppingGoods);
        if (count==1){
            return BaseResponse.generateOKResponseEntity("删除成功");
        }else {
            return BaseResponse.generateOKResponseEntity("删除失败");
        }
    }
    @ApiOperation(value = "修改商品")
    @PostMapping("/update")
    public ResponseEntity<BaseResponse<ShoppingGoods>> update(@RequestBody ShoppingGoods shoppingGoods) {

        int count = shoppingGoodsService.update(shoppingGoods);
        if (count==1){
            return BaseResponse.generateOKResponseEntity("修改成功");
        }else {
            return BaseResponse.generateOKResponseEntity("修改失败");
        }
    }
    @ApiOperation(value = "获取商品分类和品牌商列表")
    @GetMapping("/catAndBrand")
    public Object list2() {
        return shoppingGoodsService.list2();
    }

}